#!/bin/sh
# chkconfig: 2345 85 15
# description: Starts and stops the Aerospike daemon

### BEGIN INIT INFO
# Provides:          aerospike
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Aerospike Clustered Data Service
### END INIT INFO

. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0

ASD=/usr/bin/asd
ASDN=$(basename $ASD)
LOCKFILE=/var/lock/subsys/aerospike
CONFIG_FILE=/etc/aerospike/aerospike.conf
CMD="$ASD --config-file $CONFIG_FILE"
PIDDIR="/var/run/aerospike"
ASD_USER="aerospike"
ASD_GROUP=$ASD_USER
STOP_TIMEOUT=${STOP_TIMEOUT-30}
EDITION="@EDITION@"

INITFNS=/etc/aerospike/initfns
if [ -f $INITFNS ]; then . $INITFNS; fi
if [ -n $LD_PRELOAD ]; then export LD_PRELOAD; fi

# in production, the corefiles are so huge as to prevent
# quick restarts of servers. Turn this on only if requested
# DAEMON_COREFILE_LIMIT="unlimited"

set_shmall() {
	mem=`/sbin/sysctl -n kernel.shmall`
	min=4294967296
	if [ ${#mem} -le ${#min} ]; then
	    if [ $mem -lt $min ]; then
		echo "kernel.shmall too low, setting to 4G pages = 16TB"
		/sbin/sysctl -w kernel.shmall=$min
	    fi
	fi
}

set_shmmax() {
	mem=`/sbin/sysctl -n kernel.shmmax`
	min=1073741824
	if [ ${#mem} -le ${#min} ]; then
	    if [ $mem -lt $min ]; then
		echo "kernel.shmmax too low, setting to 1GB"
		/sbin/sysctl -w kernel.shmmax=$min
	    fi
	fi
}

set_listen_backlog() {
	maxc=$(cat /proc/sys/net/core/somaxconn)

	if [ ${maxc} -lt 512 ]; then
		echo "Increasing listen backlog from ${maxc} to 512"
		echo 512 >/proc/sys/net/core/somaxconn
	fi
}

set_socket_buffer_limit() {
	name=${1}; path=${2}; size=${3}
	curr=$(cat ${path})

	if [ ${curr} -lt ${size} ]; then
		echo "Increasing ${name} socket buffer limit (${path}): ${curr} -> ${size}"
		echo ${size} >${path}
	fi
}

set_socket_buffer_limits() {
	set_socket_buffer_limit read /proc/sys/net/core/rmem_max 15728640
	set_socket_buffer_limit write /proc/sys/net/core/wmem_max 5242880
}

#We are adding create_piddir as /var/run is tmpfs on some distributions. 
#This causes the piddir to be removed on reboot
#adding this to centos init for parity
create_piddir() {
	if [ ! -d $PIDDIR ]
	then
		(mkdir $PIDDIR && chown $ASD_USER:$ASD_GROUP $PIDDIR) &> /dev/null
	fi
}

start() {
	ulimit -n 100000
	logger -t aerospike  "ulimit -n="`ulimit -n`
	[ -x $ASD ] || exit 0
	set_shmall
	set_shmmax
	set_listen_backlog
	set_socket_buffer_limits
	create_piddir
	echo -n $"Starting and checking aerospike: "
	daemon "$CMD && pgrep $ASDN &> /dev/null"
	retval=$?
	echo
	[ $retval -eq 0 ] && touch $LOCKFILE
	return $retval
}

coldstart() {
	ulimit -n 100000
	logger -t aerospike  "ulimit -n="`ulimit -n`
	[ -x $ASD ] || exit 0
	set_shmall
	set_shmmax
	set_listen_backlog
	set_socket_buffer_limits
	create_piddir
	echo -n $"Cold-starting aerospike: "
	daemon "$CMD --cold-start && pgrep $ASDN &> /dev/null"
	retval=$?
	echo
	[ $retval -eq 0 ] && touch $LOCKFILE
	return $retval
}

stop() {
	echo -n $"Stopping aerospike: "
	killproc -d ${STOP_TIMEOUT} $ASDN
	retval=$?
	echo
	[ $retval -eq 0 ] && rm -f $LOCKFILE
	return $retval
}

rh_status() {
	status $ASDN
}

rh_status_quiet() {
	status $ASDN >/dev/null 2>&1
}

do_telemetry_start () {
	if [ $EDITION = "community" ];
	then
		/sbin/service aerospike_telemetry start >/dev/null 2>&1
	fi
}

do_telemetry_stop () {
	if [ $EDITION = "community" ];
	then
		/sbin/service aerospike_telemetry stop >/dev/null 2>&1
	fi
}

case "$1" in
	start)
		rh_status_quiet
		if [ $? == 0 ];
		then
			{ echo -n "Already "; $0 status; }
		else
			$1
		fi

		do_telemetry_start
		;;
	coldstart)
		rh_status_quiet
		if [ $? == 0 ];
		then
			{ echo -n "Already "; $0 status; }
		else
			$1
		fi

		do_telemetry_start
		;;
	stop)
		rh_status_quiet
		if [ $? == 3 ];
		then
			{ echo -n "Already "; $0 status; }
		else
			$1
		fi

		do_telemetry_stop
		;;
	status)
		rh_status
		;;
	restart)
		$0 stop
		sleep 3
		$0 start
		;;
	*)
		echo $"Usage: $0 {start|stop|status|coldstart|restart}"
		exit 2
		;;
esac
